package ext;

import java.util.Arrays;

import org.junit.Test;

import util.ArrayGenerator;

public class BubbleSort {
    
    public void bubble(int[] nums, int idx) {
        if (idx == 0) {
            return;
        }
        boolean flag = true;
        for (int i = 1; i <= idx; i++) {
            if (nums[i] < nums[i - 1]) {
                flag = false;
                swap(nums, i, i - 1);
            }
        }
        if (flag) {
            return;
        } else {
            bubble(nums, idx - 1);
        }
    }

    public void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }

    @Test
    public void test() {
        for (int i = 0; i < 100; i++) {
            int[] nums = ArrayGenerator.getArray(100, 10);
            bubble(nums, nums.length - 1);
            String s = Arrays.toString(nums);
            Arrays.sort(nums);
            System.out.println(Arrays.toString(nums).equals(s));
        }
    }

}
